﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Repository.Interface;
using System.Text;
using System.Threading.Tasks;
using ERP.Domain.System;
using Common.Communal;

namespace System.Services.Posts
{
    /// <summary>
    /// 岗位服务接口
    /// </summary>
    public class PostService:IPostService
    {
        /// <summary>
        /// 岗位仓储接口
        /// </summary>
        private readonly IPostRepository postRepository;

        /// <summary>
        /// 构造函数，注入岗位仓储接口
        /// </summary>
        /// <param name="postRepository">岗位仓储接口</param>
        public PostService(IPostRepository postRepository)
        {
            this.postRepository = postRepository;
        }

        /// <summary>
        /// 新增岗位
        /// </summary>
        /// <param name="post">实体</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Add(Post post)
        {
            var result = new ApiResult<int>();
            try
            {
                if (post == null)
                {
                    result.Error(result.Message = "岗位信息不能为空");
                    return result;
                }

                if (string.IsNullOrWhiteSpace(post.PostNo))
                {
                    result.Error(result.Message = "岗位编号不能为空");
                    return result;
                }

                if (post.PostNo.Length > 50)
                {
                    result.Error(result.Message = "岗位编号长度不能超过50个字符");
                    return result;
                }

                if (string.IsNullOrWhiteSpace(post.PostName))
                {
                    result.Error(result.Message = "岗位名称不能为空");
                    return result;
                }

                if (post.PostName.Length > 255)
                {
                    result.Error(result.Message = "岗位名称长度不能超过255个字符");
                    return result;
                }

                if (string.IsNullOrWhiteSpace(post.OrganizeNo))
                {
                    result.Error(result.Message = "组织机构编号不能为空");
                    return result;
                }

                // 编号唯一性检查
                var no = await postRepository.GetAsync(x => x.PostNo == post.PostNo);
                if (no.Count()>0)
                {
                    result.Error(result.Message = "岗位编号已存在");
                    return result;
                }

                var res = await postRepository.AddAsync(post);
                if (res > 0)
                {
                    result.Success(res, result.Message = "岗位新增成功");
                }
                else
                {
                    result.Error(result.Message = "岗位新增失败");
                }
            }
            catch (Exception ex)
            {
                result.Error($"新增岗位时发生错误：{ex.Message}");
            }

            return result;
        }
        
        /// <summary>
        /// 更新岗位
        /// </summary>
        /// <param name="post">实体</param>
        /// <returns>返回结果</returns>
        public async Task<ApiResult<int>> Update(Post post)
        {
            var result = new ApiResult<int>();
            try
            {
                if (post.PostNo.Length > 50)
                {
                    result.Error(result.Message = "岗位编号长度不能超过50个字符");
                    return result;
                }

                if (post.PostName.Length > 255)
                {
                    result.Error(result.Message = "岗位名称长度不能超过255个字符");
                    return result;
                } 
                // 更新
                var rows = await postRepository.Update(post);
                if (rows > 0)
                {
                    result.Success(rows, result.Message = "岗位更新成功");
                }
                else
                {
                    result.Error(result.Message = "岗位更新失败");
                }
            }
            catch (Exception ex)
            {
                result.Error($"更新岗位时发生错误：{ex.Message}");
            }

            return result;
        }

        /// <summary>
        /// 逻辑删除岗位
        /// </summary>
        /// <param name="id">岗位ID</param>
        /// <returns>操作结果</returns>
        public async Task<ApiResult<int>> Delete(int id)
        {
            var result = new ApiResult<int>();
            try
            {
                var list = await postRepository.GetAsync(x => x.Id == id);

                var post = list.FirstOrDefault();
                
                // 检查岗位是否已经被删除
                if (post.IsDeleted)
                {
                    result.Error(result.Message = "该岗位已经被删除");
                    return result;
                }

                // 执行逻辑删除（设置 IsDeleted 为 true）
                post.IsDeleted = true;
                var res = await postRepository.Update(post);

                if (res > 0)
                {
                    result.Success(res, result.Message = "岗位删除成功");
                }
                else
                {
                    result.Error(result.Message = "岗位删除失败");
                }
            }
            catch (Exception ex)
            {
                result.Error($"删除岗位时发生错误：{ex.Message}");
            }

            return result;
        }
    }
}
